export const sliderEvent = (event: Event) => {
  event.stopPropagation();
  event.preventDefault();
};

export const isInArray = (arr: Array<any>, target: any): boolean => {
  return arr.includes(target);
};

export const getElementOffset = (el: HTMLElement): { top: number, left: number } => {
  if (!el.getClientRects().length) {
    return {
      top: 0,
      left: 0
    };
  }

  const rect = el.getBoundingClientRect();
  const win = el.ownerDocument.defaultView;
  return {
    top: rect.top + win.pageYOffset,
    left: rect.left + win.pageXOffset
  };
};

export const limitNumberInRange = (value: number, min: number, max: number): number => {
  return Math.min(Math.max(value, min), max);
};

export const getPercent = (value: number, min: number, max: number): number => {
  return ((value - min) / (max - min)) * 100;
};

// 取 [min, max] 之间的值
export const getRandomInt = (range: [number, number]): number => {
  return Math.floor(Math.random() * (range[1] - range[0]) + 1) + range[0];
};

export function shuffle<T>(arr: T[]): T[] {
  const result = arr.slice(0);
  for (let i = 0; i < result.length; i++) {
    // 取 0 和 i 之间的随机值
    const j = getRandomInt([0, i]);
    [result[i], result[j]] = [result[j], result[i]];
  }
  return result;
}

